/**
 * This class is the controller for the main view for the application. It is specified as
 * the "controller" of the Main view class.
 */
Ext.define('extjs6test.view.main.MainController', {
    extend: 'Ext.app.ViewController',

    alias: 'controller.main',

    stores:["extjs6test.store.MainMenu"],

    onItemSelected: function (sender, record) {
        Ext.Msg.confirm('Confirm', 'Are you sure?', 'onConfirm', this);
    },

    onConfirm: function (choice) {
        if (choice === 'yes') {
            //
        }
    },
    onClickMaxButton: function (button, pressed) {
        var treelist = this.lookupReference('treelist'),
            ct = treelist.ownerCt;
        var logo = this.lookupReference('logo');

        if (pressed) {

            logo.animate({
                from: {
                    width: ct.getWidth()
                },
                dynamic:true,
                duration:500,
                to: {
                    width: 46
                }
            });
            this.oldWidth = ct.width;
            ct.animate({
                from: {
                    width: ct.getWidth()
                },
                dynamic:true,
                duration:500,
                to: {
                    width: 46
                },
                callback:function () {
                    treelist.setMicro(pressed);
                }
            })
        }
        else {
            treelist.setMicro(pressed);
            logo.animate({
                from: {
                    width: logo.getWidth()
                },
                dynamic:true,
                duration:500,
                to: {
                    width: this.oldWidth
                }
            });
            ct.animate({
                from: {
                    width: logo.getWidth()
                },
                dynamic:true,
                duration:500,
                to: {
                    width: this.oldWidth
                }
            })
        }
    },
    onClickQueryButton: function (btn, evt) {
        var queryField = this.lookupReference('queryField');
        if(!queryField.isVisible()){
            queryField.setWidth(0);
            queryField.show();
            queryField.animate({
                from:{
                    width:0
                },
                to:{
                    width:250
                },
                duration:500,
                dynamic:true,
                callback:function () {
                    queryField.focus();
                }
            });
        }
    },
    onNavTreeMenuSelection: function (tree, selection) {
        var contentPanel = this.lookupReference('contentPanel');
        if (selection.node.data && selection.node.data.action && Ext.decode(selection.node.data.action)
            && !contentPanel.queryById(selection.node.data.id)) {
            Ext.require(Ext.decode(selection.node.data.requires), function () {
                contentPanel.add({
                    id: selection.node.data.id,
                    itemId: selection.node.data.id,
                    title: selection.node.data.text,
                    iconCls: selection.node.data.iconCls,
                    tabConfig: {
                        closable: selection.node.data.closable == undefined || selection.node.data.closable ? true : false
                    },
                    layout: 'fit',
                    defaults:{
                        header:false
                    },
                    items: [Ext.decode(selection.node.data.action)]
                });
                contentPanel.setActiveTab(selection.node.data.id);
            });
        }
        else {
            contentPanel.setActiveTab(selection.node.data.id);
        }
    },
    onQueryFieldBlur:function (field, event, eOpts) {
       field.animate({
           from:{
               width:field.getWidth()
           },
           to:{
               width:0
           },
           duration:500,
           dynamic:true,
           callback:function () {
               field.hide()
           }
       });
    }
});
